package syllabus.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import syllabus.commons.ExtGridReturn;
import syllabus.commons.ExtPager;
import syllabus.model.Criteria;
import syllabus.model.PSyllabus;
import syllabus.service.PCourseService;
import syllabus.service.PSyllabusService;

@RequestMapping("/statistics")
@Controller
public class StatisticsAct {

	private static final Logger logger = LoggerFactory.getLogger(StatisticsAct.class);
	
	@Autowired
	private PSyllabusService syllabusService;
	@Autowired
	private PCourseService courseService;
	
	@RequestMapping("/list")
	public String list(){
		return "/statistics/list";
	}
	
	@RequestMapping(value = "/all", method = RequestMethod.POST)
	@ResponseBody
	public Object all(ExtPager pager,
			HttpServletRequest request,ModelMap map) {

		Criteria criteria = new Criteria();
		// 设置分页信息
		if (pager.getPage() != null && pager.getRows() != null) {
			criteria.setPage(pager.getPage());
			criteria.setPageSize(pager.getRows());
		}
		// 排序信息
		if (StringUtils.isNotBlank(pager.getOrder())
				&& StringUtils.isNotBlank(pager.getSort())) {
			criteria.setOrderByClause(pager.getSort() + " " + pager.getOrder());
		}

		
		int syllabusTotal = this.syllabusService.countByExample(criteria);
		int courseTotal = this.courseService.countByExample(criteria);
		
		criteria.put("linkIsNotNull", "IS NOT NULL");

		int num1 = this.syllabusService.countByExample(criteria);
		
		List<Statistic> list = new ArrayList<Statistic>();
		Statistic s = new Statistic(syllabusTotal, num1,courseTotal);
		list.add(s);
		
		return new ExtGridReturn(1, list);
	}
	
	class Statistic{
		private int total;
		private int num;
		private int kcNum;
		
		public Statistic(int total,int num,int kcNum){
			this.total = total;
			this.num = num;
			this.kcNum = kcNum;
		}

		public int getTotal() {
			return total;
		}

		public void setTotal(int total) {
			this.total = total;
		}

		public int getNum() {
			return num;
		}

		public void setNum(int num) {
			this.num = num;
		}

		public int getKcNum() {
			return kcNum;
		}

		public void setKcNum(int kcNum) {
			this.kcNum = kcNum;
		}
		
	}
}
